/**
* Created by wust on 2019/5/14.
*/
<template xmlns:v-data-dictionary-directive="http://www.w3.org/1999/xhtml">
  <el-form ref="dataModel" :model="dataModel" :rules="rules" label-width="120px" style="width: 70%;" @submit.native.prevent>
    <el-form-item label="选择菜单" prop="menuCode">
      <ul id="ztree" class="ztree"></ul>
    </el-form-item>
    <el-form-item label="快捷菜单名称" prop="name">
      <el-input v-model="dataModel.name"></el-input>
    </el-form-item>
    <el-form-item label="备注">
      <el-input
        type="textarea"
        :rows="6"
        placeholder="请输入内容"
        maxlength="255"
        show-word-limit
        v-model="dataModel.description">
      </el-input>
    </el-form-item>
    <el-form-item style="text-align: left;">
      <el-button type="primary" @click="onSubmit('dataModel')" :loading="submitting">提交</el-button>
    </el-form-item>
  </el-form>
</template>
<script>
import Vue from 'vue'
import '../../../../static/zTree_v3.5.27/js/jquery.ztree.core.js'
import '../../../../static/zTree_v3.5.27/js/jquery.ztree.excheck.js'
import '../../../../static/zTree_v3.5.27/js/jquery.ztree.exedit.js'

export default {
  name: 'shortcut-menu-create',
  data () {
    return {
      submitting: false,
      dataModel: {
        name: '',
        menuCode: '',
        description: ''
      },
      setting: {
        view: {
          showLine: true
        },
        check: {
          enable: true,
          chkStyle: 'radio',
          radioType: 'all'
        },
        data: {
          simpleData: {
            enable: true
          }
        },
        callback: {
          beforeClick: (treeId, treeNode, clickFlag) => {

          },
          onCheck: (event, treeId, treeNode) => {
            this.dataModel.menuCode = treeNode.id
            this.dataModel.name = treeNode.name
          }
        }
      },
      rules: {
        menuCode: [{required: true, message: '请选择菜单', trigger: 'blur'}],
        name: [{required: true, message: '快捷菜单名称不能为空', trigger: 'blur'}]
      }
    }
  },
  created: function () {
    this.initMenuTree()
  },
  methods: {
    initMenuTree: function () {
      Vue.$ajax({
        method: 'post',
        url: Vue.$adminServerURL + '/web/v1/ShortcutMenuController/buildMenuTree'
      }).then(res => {
        if (res.data.flag === 'SUCCESS') {
          let zNodes = JSON.parse(res.data.obj)
          // eslint-disable-next-line no-undef
          $.fn.zTree.init($('#ztree'), this.setting, zNodes)
        } else {
          if (!Vue.$isNullOrIsBlankOrIsUndefined(res.data.message)) {
            this.$message({
              message: res.data.message,
              type: 'warning'
            })
          }
        }
      })
    },
    onSubmit: function (formData) {
      this.$refs[formData].validate((valid) => {
        if (!valid) {
        } else {
          this.submitting = true
          Vue.$ajax({
            method: 'post',
            url: Vue.$adminServerURL + '/web/v1/ShortcutMenuController',
            data: this.dataModel
          }).then(res => {
            this.submitting = false
            if (res.data.flag !== 'SUCCESS') {
              if (!Vue.$isNullOrIsBlankOrIsUndefined(res.data.message)) {
                this.$message({
                  message: res.data.message,
                  type: 'warning'
                })
              }
            } else {
              this.$message({
                message: res.data.message,
                type: 'success'
              })
              this.resetForm('dataModel')
            }
          })
        }
      })
    },
    resetForm (formData) {
      this.$refs[formData].resetFields()
    }
  }
}
</script>
<style>
  @import '../../../../static/zTree_v3.5.27/css/zTreeStyle.css';
</style>
